<?php

/*
 * Atheme PHP Class
 * (C) 2010 Global IRC Network
 * 
 * Lead developer : rbonkestoter (rick@globalirc.net)
 */

include('xmlfunction.php');
include('class.athemephp_nickserv.php');
include('class.athemephp_chanserv.php');
include('class.athemephp_memoserv.php');
include('class.athemephp_hostserv.php');

class AthemePHP {

	public $NickServ;
	public $ChanServ;
	public $MemoServ;
	public $HostServ;
	public $xmlURL;
	private $params = array();
	public $include = true;

	/**
	 * 
	 * @param $xmlrpc
	 * @param $include
	 * @return unknown_type
	 */
	function AthemePHP($xmlrpc, $include = true)
	{
		// Constructor
		// Get the XMLRPC url
		$this->xmlURL = $xmlrpc;
		$this->include = $include;
		// Construct NickServ
		if($this->include)
		{
			$this->NickServ = new NickServ($xmlrpc);
			$this->ChanServ = new ChanServ($xmlrpc);
			$this->MemoServ = new MemoServ($xmlrpc);
			$tihs->HostServ = new HostServ($xmlrpc);
		}
	}

	/**
	 * 
	 * @param $atheme_nick
	 * @param $atheme_id
	 * @return unknown_type
	 */
	function doCMD($atheme_nick = 'x', $atheme_id = '.')
	{
		return $this->DoCommand($atheme_nick, $atheme_id);
	}
	
	function DoCommand($atheme_nick = 'x', $atheme_id = '.', $command = 'atheme.command')
	{
		$cmd = "<?xml version=\"1.0\"?>
<methodCall>
   <methodName>".$command."</methodName>
   <params>
      <param>
         <value><string>".$atheme_id."</string></value>
         </param>
         <param>
         <value><string>".$atheme_nick."</string></value>
         </param>
         <param>
         <value><string>".$_SERVER['REMOTE_ADDR']."</string></value>
         </param>".$this->parseParams()."</params>
   </methodCall>";
		
		$retStr = $this->get_response($cmd);
		$result = XML_unserialize($retStr);

		return $result;
	}
	
	/**
	 * 
	 * @param $xml
	 * @return unknown_type
	 */
	function get_response($xml) {
        $len = strlen($xml);
        $trg = curl_init();
        curl_setopt($trg, CURLOPT_URL, $this->xmlURL);
		curl_setopt($trg, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($trg, CURLOPT_TIMEOUT, 4);
		curl_setopt($trg, CURLOPT_POSTFIELDS, $xml);
		curl_setopt($trg, CURLINFO_HEADER_OUT, TRUE);
		curl_setopt($trg, CURLOPT_HTTPHEADER, array('Connection: close', 'Content-Type: text/xml'));
        $reg= curl_exec($trg);
        curl_close($trg);
        return $reg;
	}
	
	/* PARAM FUNC */
	/**
	 * 
	 * @param $param
	 * @return unknown_type
	 */
	function addParam($param)
	{
		$this->params[] = $param;
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	private function parseParams()
	{
		$retString = "";
		foreach($this->params as $param)
		{
			$retString .= "<param><value><string>".$param."</string></value></param>";
		}
		
		return $retString;
	}
	
	/* END OF PARAM FUNC */

	/** Keep alive **/
	
	/**
	 * 
	 * @param $type
	 * @param $username
	 * @param $password
	 * @return unknown_type
	 */
	
	// TESTING ONLY!!!
	function KeepAlive($type = 'cookie', $username, $password)
	{
		$values = (object)array("username" => $username,  "password" => $password);
		switch($type)
		{
			case 'cookie':
				setcookie('__ATHEMEPHP__KEEP__ALIVE__', $values, time()+3600);
				break;
				
			case 'session':
				$_SESSION['__ATHEMEPHP__KEEP__ALIVE__'] = $values;
				break;
		}
	}
}

?>